package com.hktf.corelib.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

/**
 * SharePre  工具类
 *
 * @author Administrator
 */
public class SharePreUtils {
    private SharePreUtils() {
    }

    private static final String PREF_NAME = "config";//基本配置文件

    public static final String USER_INFO = "user_info";//用户信息表

    public static String USER_NAME = "user_name";//登录用户的账号
    public static String IS_USER_GUIDE_SHOWED = "is_user_guide_showed";
    public static String IS_LINK_VDI = "is_link_vdi";//新用户是否连接过VDI
    public static String IS_BIND_VDI = "is_bind_vdi";//用户是否绑定过VDI
    public static String SCREENSTATE = "is_screen_Orientation";
    public static String USEPOSITION = "use_position";//记录用户使用应用的位置信息

    public static String PERMISSIONTIME = "PermissionTime"; //更新车型权限时间
    public static String LASTRUNTIME = "LastRunTime"; //最后一次运行时间
    private static SharedPreferences sp;

    public static String ADPOSITION = "adposition";

    public static String UPDATEDATE = "UpdateDate"; //APP更新时间
    public static String SEARCH_HISTORY = "search_history"; //搜索历史记录

    public static int getADPosition(Context context, String key, int defValue) {
        if (sp == null) {
            sp = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
        }

        return sp.getInt(key, defValue);
    }


    public static void setADPosition(Context context, String key, int value) {
        if (sp == null) {
            sp = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
        }
        Editor edit = sp.edit();
        edit.putInt(key, value);
        edit.commit();
    }


    public static boolean getBoolean(Context context, String key, boolean defValue) {
        if (sp == null) {
            sp = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
        }
        return sp.getBoolean(key, defValue);
    }

    public static void setBoolean(Context context, String key, boolean value) {
        if (sp == null) {
            sp = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
        }
        Editor edit = sp.edit();
        edit.putBoolean(key, value);
        edit.commit();
    }

    /**
     * hexunwu TODO 用于缓存记录登录用户的用户名
     */
    public static String getString(Context context, String key, String defValue) {
        if (sp == null) {
            sp = context.getSharedPreferences(USER_INFO, Context.MODE_PRIVATE);
        }
        return sp.getString(key, defValue);
    }

    public static void setString(Context context, String key, String value) {
        if (sp == null) {
            sp = context.getSharedPreferences(USER_INFO, Context.MODE_PRIVATE);
        }
        Editor edit = sp.edit();
        edit.putString(key, value);
        edit.commit();
    }

    /**
     * hexunwu TODO 用于记录用户是不是首次使用蓝牙连接,用于判断是不是显示使用指导
     */
    public static boolean getFunctionGuide(Context context, String key, boolean defValue) {
        if (sp == null) {
            sp = context.getSharedPreferences(USER_INFO, Context.MODE_PRIVATE);
        }
        return sp.getBoolean(key, defValue);
    }

    public static void setFunctionGuide(Context context, String key, boolean value) {
        if (sp == null) {
            sp = context.getSharedPreferences(USER_INFO, Context.MODE_PRIVATE);
        }
        Editor edit = sp.edit();
        edit.putBoolean(key, value);
        edit.commit();
    }

    /**
     * hexuwnu TODO 用于记录用户是否绑定过VDI
     */
    public static boolean getIsBindVdi(Context context, String key, boolean defValue) {
        if (sp == null) {
            sp = context.getSharedPreferences(USER_INFO, Context.MODE_PRIVATE);
        }
        return sp.getBoolean(key, defValue);
    }

    public static void setIsBindVdi(Context context, String key, boolean value) {
        if (sp == null) {
            sp = context.getSharedPreferences(USER_INFO, Context.MODE_PRIVATE);
        }
        Editor edit = sp.edit();
        edit.putBoolean(key, value);
        edit.commit();
    }

    /**
     * TODO 用于保存当前屏幕的方向
     *
     * @param context
     * @param key
     * @param defValue
     * @return
     */
    public static boolean getScreenState(Context context, String key, boolean defValue) {
        if (sp == null) {
            sp = context.getSharedPreferences(USER_INFO, Context.MODE_PRIVATE);
        }
        return sp.getBoolean(key, defValue);
    }

    public static void setScreenSate(Context context, String key, boolean value) {
        if (sp == null) {
            sp = context.getSharedPreferences(USER_INFO, Context.MODE_PRIVATE);
        }
        Editor edit = sp.edit();
        edit.putBoolean(key, value);
        edit.commit();
    }

    /**
     * hexunwu TODO 用于缓存记录用户使用的位置信息
     */
    public static String getPosition(Context context, String key, String defValue) {
        if (sp == null) {
            sp = context.getSharedPreferences(USER_INFO, Context.MODE_PRIVATE);
        }
        return sp.getString(key, defValue);
    }

    public static void setPosition(Context context, String key, String value) {
        if (sp == null) {
            sp = context.getSharedPreferences(USER_INFO, Context.MODE_PRIVATE);
        }
        Editor edit = sp.edit();
        edit.putString(key, value);
        edit.commit();
    }


    /**
     * hexunwu TODO 用于缓存用户更新、获取权限、最后一次使用的时间
     */
    public static String getUseTime(Context context, String key, String defValue) {
        if (sp == null) {
            sp = context.getSharedPreferences(USER_INFO, Context.MODE_PRIVATE);
        }
        return sp.getString(key, defValue);
    }

    public static void setUseTime(Context context, String key, String value) {
        if (sp == null) {
            sp = context.getSharedPreferences(USER_INFO, Context.MODE_PRIVATE);
        }
        Editor edit = sp.edit();
        edit.putString(key, value);
        edit.commit();
    }


}
